import 'dart:async';
import 'dart:convert';
import 'dart:ui';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import '../../util/Toast.dart';
import '../../base/Constant.dart';
// import 'package:panda/base/SignType.dart';
import '../../base/application.dart';
import '../../entity/gallery_entity.dart';
import '../goods/ShareGoodsPic.dart';
import '../goods/sku_dialog.dart';
import '../../liz_car/MyShopCar.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import '../../login/UserLogin.dart';
import '../../liz_mine/order/OrderPage.dart';
import '../../liz_mine/set/FillAuthCode.dart';
import '../../util/AppSaveInfoUtil.dart';
import '../../util/SignUtil.dart';
import 'package:provider/provider.dart';
import '../../liz_home/goods/BuyGoods.dart';
import '../../liz_home/goods/GoodsDetailsModel.dart';
import '../../liz_home/goods/SkuProvider.dart';

class QGGoodsDetailPage extends StatefulWidget {
  String goods_id = "";
  String img;
  int type = 1; //默认非抢购
  String id = "0";
  String start_time = "",
      end_time = "",
      limit_num = "",
      all_num = "",
      sale_num = "";
  QGGoodsDetailPage(
      {Key key,
      @required this.goods_id,
      @required this.img,
      this.type,
      this.id,
      this.all_num,
      this.end_time,
      this.limit_num,
      this.sale_num,
      this.start_time})
      : super(key: key);

  _QGGoodsDetailPageState createState() => _QGGoodsDetailPageState();
}

class _QGGoodsDetailPageState extends State<QGGoodsDetailPage> {
  double appBarAlpha = 0;
  SkuProvider skuProvider;
  Object price = "";
  Object old_price = "";
  String goods_name = "", lv_price = "";
  String sales_num = "0";
  String html = "";
  String freight = "";
  List<GalleryModel> gallery = List();
  bool hasSku = false;
  int count = 0;
  int goods_num = 1;
  String token = "";
  Map<String, dynamic> skuMap = {"skus": [], "goods": []};
  GoodsDetailsModel goodsDetailsModel;
  Timer _timerIndex;
  String _day1 = '';
  String _hour1 = '';
  String _minute1 = '';
  String _second1 = '';
  _onScroll(offset) {
    double alpha = offset / 50;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
      getGoodsDetail();
    });
    if (widget.type == 1) {
      _timeFunc(widget.end_time);
    }
    GalleryModel model = new GalleryModel(img: widget.img);
    gallery.add(model);
    count = 1;
    goodsDetailsModel = GoodsDetailsModel.fromJson(skuMap);
    skuProvider = SkuProvider(goodsDetailsModel);
    var list =
        goodsDetailsModel.goods.map((item) => item.attribute_name).toList();
    skuProvider.selectedSku = "请选择\t${list.join("，")}";
  }

  getGoodsDetail() async {
    Map map = new Map();
    map['goods_id'] = widget.id;
    // map['token'] = token;
    map['sign_type'] = createSignType(Api.URL_GOODSFS_MSG);
    Response<String> response =
        await HttpUtil().post(Api.URL_GOODSFS_MSG, data: getSign(map));
    Map userMap = json.decode(response.data);
    print('=====抢购商品');
    print(response);
    int code = userMap['code'];
    if (code == 1 && mounted) {
      // gallery.clear();
      // if (userMap['data']['imglist'] == null ||
      //     (userMap['data']['imglist'] as List).length == 0) {
      //   GalleryModel model =
      //       new GalleryModel(img: userMap['data']['goodsMsg']['img']);
      //   gallery.add(model);
      // } else {
      //   (userMap['data']['imglist'] as List).forEach((v) {
      //     gallery.add(new GalleryModel.fromJson(v));
      //   });
      // }
      // setState(() {
      //   count = gallery.length;
      // });
      setState(() {
        html = userMap['data']['goodsMsg']['content'] == null
            ? ""
            : userMap['data']['goodsMsg']['content'];
        goods_name = userMap['data']['goodsMsg']['goods_name'];
        sales_num = userMap['data']['goodsMsg']['sales_num'].toString();
        price = userMap['data']['goodsMsg']['price'];
        freight = userMap['data']['goodsMsg']['detail']['postage'].toString();
        old_price = userMap['data']['goodsMsg']['detail']['old_price'];
        // lv_price = userMap['data']['goodsMsg']['lv_price'].toString();
        // print(userMap['data']['goodsMsg']['sku_arr'] ?? "");
        // print((userMap['data']['skulist'].toString()) ?? "");
        if (userMap['data']['skulist'] != null &&
            (userMap['data']['skulist'] as List).length > 0) {
          setState(() {
            hasSku = false;
            skuMap['skus'] = userMap['data']['skulist'];
            skuMap['goods'] = userMap['data']['goodsMsg']['sku_arr'];
            goodsDetailsModel = GoodsDetailsModel.fromJson(skuMap);
            skuProvider = SkuProvider(goodsDetailsModel);
            var list = goodsDetailsModel.goods
                .map((item) => item.attribute_name)
                .toList();
            skuProvider.selectedSku = "请选择\t${list.join("，")}";
          });
        } else {
          setState(() {
            hasSku = true;
          });
        }
      });
    }
  }

  void _timeFunc(time) {
    var _newDate = DateTime.now();
    const period = const Duration(seconds: 1);
    var _diffDate = DateTime.parse(time.toString());
    _timerIndex = Timer.periodic(period, (timer) {
      //到时回调
      _diffDate = _diffDate.subtract(Duration(seconds: 1));
      // count++;
      if (_diffDate.difference(_newDate).inSeconds <= 0) {
        //取消定时器，避免无限回调
        timer.cancel();
        timer = null;
      }
      // print();
      var _surplus = _diffDate.difference(_newDate);
      int _day = (_surplus.inSeconds ~/ 3600) ~/ 24;
      int hour = (_surplus.inSeconds ~/ 3600) % 24;
      int minute = _surplus.inSeconds % 3600 ~/ 60;
      int second = _surplus.inSeconds % 60;
      // formatTime(hour) + ":" + formatTime(minute) + ":" + formatTime(second);
      setState(() {
        _day1 = _day.toString() ?? '0';
        _hour1 = hour.toString() ?? '0';
        _minute1 = minute.toString() ?? '0';
        _second1 = second.toString() ?? '0';
        _day1 = _day1.length > 1 ? _day1 : ("0" + _day1);
        _hour1 = _hour1.length > 1 ? _hour1 : ("0" + _hour1);
        _minute1 = _minute1.length > 1 ? _minute1 : ("0" + _minute1);
        _second1 = _second1.length > 1 ? _second1 : ("0" + _second1);
        // _text = _diffDate.weekday.toString()+'  ' +_diffDate.hour.toString()+':' + _diffDate.minute.toString() +':'+ _diffDate.second.toString();
      });
      // debugPrint(_text);
    });
  }

  @override
  void dispose() {
    super.dispose();
    _timerIndex.cancel();
    _timerIndex = null;
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);

    return new Scaffold(
      appBar: new AppBar(
        title: Text('商品详情',
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.black)),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
        elevation: 0.5,
        // actions: <Widget>[
        //   GestureDetector(
        //     child: Image.asset("images/icon_nav_share.png"),
        //   )
        // ],
      ),
      body: ChangeNotifierProvider.value(
          value: skuProvider,
          child: Consumer<SkuProvider>(builder: (context, provider, _) {
            return new Column(
              children: <Widget>[
                Expanded(
                  child: Container(
                      child: new Scrollbar(
                    child: new SingleChildScrollView(
                      child: new Container(
                        child: new Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Stack(
                              children: <Widget>[
                                Container(
                                  height: MediaQuery.of(context).size.width,
                                  child: Swiper(
                                    containerHeight:
                                        MediaQuery.of(context).size.width,
                                    itemHeight:
                                        MediaQuery.of(context).size.width,
                                    itemBuilder:
                                        (BuildContext context, int index) {
                                      return new ClipRRect(
                                        child: gallery[index].img == ""
                                            ? Image.asset(
                                                'images/logo.png',
                                                fit: BoxFit.fill,
                                              )
                                            : gallery[index]
                                                    .img
                                                    .contains("upload")
                                                ? Image.network(
                                                    Api.BaseUrl2 +
                                                        gallery[index]
                                                            .img
                                                            .replaceAll(
                                                                '\\', '/'),
                                                    fit: BoxFit.fill,
                                                  )
                                                : Image.network(
                                                    Api.ImgBaseUrl +
                                                        gallery[index]
                                                            .img
                                                            .replaceAll(
                                                                '\\', '/'),
                                                    fit: BoxFit.fill,
                                                  ),
                                        borderRadius:
                                            BorderRadius.circular(0.0),
                                      );
                                    },
                                    itemCount: count,
                                    pagination: new SwiperPagination(
                                        builder: DotSwiperPaginationBuilder(
                                            color: Colors.grey,
                                            activeColor: mainColor,
                                            size: 7.0,
                                            activeSize: 7.0)),
                                    scrollDirection: Axis.horizontal,
                                    autoplay: true,
                                    onTap: (index) {
                                      // Navigator.of(context)
                                      //     .push(new MaterialPageRoute(builder: (context) {
                                      //   return new HomeWebview(
                                      //     article_id: bannerData[index].img,
                                      //     title: bannerData[index].title,
                                      //   );
                                      // }));
                                    },
                                  ),
                                ),
                                Positioned(
                                    child: Container(
                                      width: ScreenUtil.getInstance().setWidth(750),
                                      decoration: BoxDecoration(
                                          color: Color.fromARGB(255, 255, 140, 16),
                                      ),
                                      alignment: Alignment.centerRight,
                                      child: Row(
                                        children: <Widget>[
                                          Container(
                                            decoration: BoxDecoration(
                                              color: Colors.white,
                                              borderRadius:
                                                  BorderRadius.circular(8.0),
                                            ),
                                            margin: EdgeInsets.only(left: 10),
                                            padding: EdgeInsets.all(2),
                                            child: Text(
                                              '限时购',
                                              style: TextStyle(
                                                  color: Colors.red,
                                                  fontSize:
                                                      ScreenUtil.getInstance()
                                                          .setSp(20)),
                                            ),
                                          ),
                                          Container(
                                            margin: EdgeInsets.only(left: 10),
                                            child: Text(MONEY + price,
                                                style: TextStyle(
                                                    color: Colors.white,
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(40),
                                                    fontWeight:
                                                        FontWeight.w600)),
                                          ),
                                          Expanded(
                                            child: Container(
                                              margin: EdgeInsets.only(left: 5),
                                              child: Text(MONEY + old_price,
                                                  style: TextStyle(
                                                      color: Colors.white,
                                                      fontSize: ScreenUtil
                                                              .getInstance()
                                                          .setSp(20),
                                                      decoration: TextDecoration
                                                          .lineThrough,
                                                      fontWeight:
                                                          FontWeight.w600)),
                                            ),
                                          ),
                                          Container(
                                              padding: EdgeInsets.fromLTRB(
                                                  ScreenUtil.getInstance()
                                                      .setHeight(DP_15),
                                                  ScreenUtil.getInstance()
                                                      .setHeight(DP_10),
                                                  ScreenUtil.getInstance()
                                                      .setHeight(DP_15),
                                                  (ScreenUtil.getInstance()
                                                      .setHeight(DP_10))),
                                              color: Color.fromRGBO(
                                                  248, 217, 167, 1),
                                              child: Column(
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.end,
                                                  children: <Widget>[
                                                    Text(
                                                        "共" +
                                                            widget.all_num
                                                                .toString() +
                                                            "件   已抢" +
                                                            widget.sale_num
                                                                .toString() +
                                                            "件",
                                                        style: TextStyle(
                                                          fontSize: ScreenUtil
                                                                  .getInstance()
                                                              .setSp(
                                                                  SMALL_FONT_SIZE),
                                                          color: Colors.red,
                                                        )),
                                                    Text("距离活动结束",
                                                        style: TextStyle(
                                                          fontSize: ScreenUtil
                                                                  .getInstance()
                                                              .setSp(
                                                                  MINI_FONT_SIZE),
                                                          color: mainColor,
                                                        )),
                                                    Container(
                                                        padding:
                                                            EdgeInsets.only(
                                                                top: 3),
                                                        child: Row(
                                                            crossAxisAlignment:
                                                                CrossAxisAlignment
                                                                    .end,
                                                            mainAxisAlignment:
                                                                MainAxisAlignment
                                                                    .end,
                                                            children: <Widget>[
                                                              Container(
                                                                padding:
                                                                    EdgeInsets
                                                                        .all(2),
                                                                decoration: BoxDecoration(
                                                                    color:
                                                                        mainColor,
                                                                    borderRadius:
                                                                        BorderRadius.circular(
                                                                            3)),
                                                                child: Text(
                                                                    _day1,
                                                                    style:
                                                                        TextStyle(
                                                                      fontSize: ScreenUtil
                                                                              .getInstance()
                                                                          .setSp(
                                                                              MINI_FONT_SIZE),
                                                                      color:
                                                                          white,
                                                                    )),
                                                              ),
                                                              Padding(
                                                                padding:
                                                                    EdgeInsets
                                                                        .all(3),
                                                                child: Text("天",
                                                                    style:
                                                                        TextStyle(
                                                                      fontSize: ScreenUtil
                                                                              .getInstance()
                                                                          .setSp(
                                                                              MINI_FONT_SIZE),
                                                                      color:
                                                                          mainColor,
                                                                    )),
                                                              ),
                                                              Container(
                                                                padding:
                                                                    EdgeInsets
                                                                        .all(2),
                                                                decoration: BoxDecoration(
                                                                    color:
                                                                        mainColor,
                                                                    borderRadius:
                                                                        BorderRadius.circular(
                                                                            3)),
                                                                child: Text(
                                                                    _hour1,
                                                                    style:
                                                                        TextStyle(
                                                                      fontSize: ScreenUtil
                                                                              .getInstance()
                                                                          .setSp(
                                                                              MINI_FONT_SIZE),
                                                                      color:
                                                                          white,
                                                                    )),
                                                              ),
                                                              Padding(
                                                                padding:
                                                                    EdgeInsets
                                                                        .all(3),
                                                                child: Text(":",
                                                                    style:
                                                                        TextStyle(
                                                                      fontSize: ScreenUtil
                                                                              .getInstance()
                                                                          .setSp(
                                                                              MINI_FONT_SIZE),
                                                                      color:
                                                                          mainColor,
                                                                    )),
                                                              ),
                                                              Container(
                                                                padding:
                                                                    EdgeInsets
                                                                        .all(2),
                                                                decoration: BoxDecoration(
                                                                    color:
                                                                        mainColor,
                                                                    borderRadius:
                                                                        BorderRadius.circular(
                                                                            3)),
                                                                child: Text(
                                                                    _minute1,
                                                                    style:
                                                                        TextStyle(
                                                                      fontSize: ScreenUtil
                                                                              .getInstance()
                                                                          .setSp(
                                                                              MINI_FONT_SIZE),
                                                                      color:
                                                                          white,
                                                                    )),
                                                              ),
                                                              Padding(
                                                                padding:
                                                                    EdgeInsets
                                                                        .all(3),
                                                                child: Text(":",
                                                                    style:
                                                                        TextStyle(
                                                                      fontSize: ScreenUtil
                                                                              .getInstance()
                                                                          .setSp(
                                                                              MINI_FONT_SIZE),
                                                                      color:
                                                                          mainColor,
                                                                    )),
                                                              ),
                                                              Container(
                                                                padding:
                                                                    EdgeInsets
                                                                        .all(2),
                                                                decoration: BoxDecoration(
                                                                    color:
                                                                        mainColor,
                                                                    borderRadius:
                                                                        BorderRadius.circular(
                                                                            3)),
                                                                child: Text(
                                                                    _second1,
                                                                    style:
                                                                        TextStyle(
                                                                      fontSize: ScreenUtil
                                                                              .getInstance()
                                                                          .setSp(
                                                                              MINI_FONT_SIZE),
                                                                      color:
                                                                          white,
                                                                    )),
                                                              ),
                                                            ]))
                                                  ]))
                                        ],
                                      ),
                                    ),
                                    bottom: 0,
                                    right: 0),
                              ],
                            ),
                            Container(
                              color: white,
                              alignment: Alignment.centerLeft,
                              child: Padding(
                                padding: EdgeInsets.fromLTRB(10, 10, 10, 5),
                                child: Text(goods_name,
                                    style: TextStyle(
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(NORMAL_FONT_SIZE),
                                      color: black,
                                    )),
                              ),
                            ),

                            // Container(
                            //   color: white,
                            //   width: double.infinity,
                            //   child: Text('已售 $sales_num 件',
                            //       style: TextStyle(
                            //         fontSize: ScreenUtil.getInstance()
                            //             .setSp(SMALL_FONT_SIZE),
                            //         color: Colors.black54,
                            //       )),
                            //   padding: EdgeInsets.only(
                            //       left:
                            //           ScreenUtil.getInstance().setHeight(DP_15),
                            //       bottom: 5),
                            // ),
                            Container(
                              height: 50,
                              padding: EdgeInsets.all(10),
                              color: white,
                              child: new GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                child: new Row(
                                  children: <Widget>[
                                    Padding(
                                      padding: EdgeInsets.fromLTRB(0, 0, 15, 0),
                                      child: Text("限购",
                                          style: TextStyle(
                                              color: Colors.red,
                                              fontSize: ScreenUtil.getInstance()
                                                  .setSp(SMALL_FONT_SIZE + 2),
                                              fontWeight: FontWeight.w600)),
                                    ),
                                    Expanded(
                                      child: Text('每人限购1单 每单限购1份',
                                          style: TextStyle(
                                            color: black_99,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(SMALL_FONT_SIZE),
                                          )),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            Container(
                              height: 2,
                              color: divideColor,
                            ),
                            Container(
                              height: 50,
                              padding: EdgeInsets.all(10),
                              color: white,
                              child: new GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                onTap: () {
                                  showGoodsAttributes(context);
                                },
                                child: new Row(
                                  children: <Widget>[
                                    Padding(
                                      padding: EdgeInsets.fromLTRB(0, 0, 15, 0),
                                      child: Text("选择",
                                          style: TextStyle(
                                            color: black,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(SMALL_FONT_SIZE + 1),
                                          )),
                                    ),
                                    Expanded(
                                      child: Text(provider.selectedSku ?? '',
                                          style: TextStyle(
                                            color: black_99,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(SMALL_FONT_SIZE),
                                          )),
                                    ),
                                    Icon(
                                      Icons.arrow_forward_ios,
                                      size: 15,
                                    )
                                  ],
                                ),
                              ),
                            ),
                            Container(
                              height: 2,
                              color: divideColor,
                            ),
                            Container(
                              height: 50,
                              padding: EdgeInsets.all(10),
                              color: white,
                              child: new GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                onTap: () {
                                  // showGoodsAttributes(context);
                                },
                                child: new Row(
                                  children: <Widget>[
                                    Padding(
                                      padding: EdgeInsets.fromLTRB(0, 0, 15, 0),
                                      child: Text("运费    ",
                                          style: TextStyle(
                                            color: black,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(SMALL_FONT_SIZE + 1),
                                          )),
                                    ),
                                    Expanded(
                                      child: Text(MONEY + '$freight',
                                          style: TextStyle(
                                            color: black_99,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(SMALL_FONT_SIZE),
                                          )),
                                    ),
                                    // Icon(
                                    //   Icons.arrow_forward_ios,
                                    //   size: 15,
                                    // )
                                  ],
                                ),
                              ),
                            ),
                            Container(
                              height: 10,
                              color: divideColor,
                            ),
                            new Row(
                              children: <Widget>[
                                Expanded(
                                  child: Container(
                                    margin: EdgeInsets.fromLTRB(
                                        ScreenUtil.getInstance().setWidth(150),
                                        0,
                                        20,
                                        0),
                                    height: 1,
                                    color: Colors.grey,
                                  ),
                                ),
                                Text(
                                  "商品详情",
                                  textAlign: TextAlign.right,
                                  style: new TextStyle(
                                      color: Colors.grey, fontSize: 14),
                                ),
                                Expanded(
                                  child: Container(
                                    margin: EdgeInsets.fromLTRB(
                                        20,
                                        0,
                                        ScreenUtil.getInstance().setWidth(150),
                                        0),
                                    height: 1,
                                    color: Colors.grey,
                                  ),
                                ),
                              ],
                            ),
                            Container(
                              color: white,
                              padding: EdgeInsets.all(10),
                              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
                              child: Html(data: html),
                            )
                          ],
                        ),
                      ),
                    ),
                  )),
                ),
                Container(
                  color: white,
                  padding: EdgeInsets.fromLTRB(20, 2, 20, 5),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      // GestureDetector(
                      //   onTap: () {
                      //     List<String> temp = new List();
                      //     for (int i = 0; i < gallery.length; i++) {
                      //       temp.add(gallery[i].img);
                      //     }
                      //     Navigator.of(context)
                      //         .push(new MaterialPageRoute(builder: (context) {
                      //       return new ShareGoodsPicPage(
                      //         pics: temp,
                      //       );
                      //     }));
                      //   },
                      //   behavior: HitTestBehavior.opaque,
                      //   child: new Column(
                      //     children: <Widget>[
                      //       Padding(
                      //         padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                      //         child: Image.asset('images/icon_nav_share.png',
                      //             scale: 1.7),
                      //       ),
                      //       Text('分享',
                      //           style: TextStyle(
                      //               fontSize: ScreenUtil.getInstance()
                      //                   .setSp(SMALL_FONT_SIZE - 4)))
                      //     ],
                      //   ),
                      // ),
                      Container(
                        width: 20,
                      ),
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          // showGoodsAttributes(context);
                          Navigator.of(context)
                              .push(new MaterialPageRoute(builder: (context) {
                            return new MyShopCarPage();
                          }));
                        },
                        child: new Column(
                          children: <Widget>[
                            Padding(
                              padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                              child: Image.asset(
                                  'images/icon_goodstab_shoppingcart.png',
                                  scale: 1.6),
                            ),
                            Text('购物车', style: TextStyle(fontSize: 11))
                          ],
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Text(''),
                      ),
                      Offstage(
                        offstage: widget.type != null,
                        child: new Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                          height: ScreenUtil.getInstance().setHeight(50),
                          decoration: new BoxDecoration(
                              color: Colors.orangeAccent,
                              borderRadius: BorderRadius.circular(
                                  ScreenUtil.getInstance().setHeight(5))),
                          child: new FlatButton(
                            child: new Padding(
                              padding: EdgeInsets.all(0.0),
                              child: new Text(
                                "加入购物车",
                                style: new TextStyle(
                                    color: Colors.white,
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(SMALL_FONT_SIZE)),
                              ),
                            ),
                            onPressed: () {
                              // Navigator.of(context).pop();
                              if (provider.selectedSku.contains("请选择") &&
                                  hasSku == false) {
                                showGoodsAttributes(context);
                                return;
                              }
                              if (hasSku) {
                                addToCar("");
                                return;
                              }
                              List<Map> skuMap = List();
                              List<Goods> list = goodsDetailsModel.goods;
                              for (int i = 0; i < list.length; i++) {
                                Map goodsSku = new Map();
                                goodsSku['attribute_id'] = list[i].attribute_id;
                                goodsSku['value'] = skuProvider
                                    .selectedSkuModel.sku
                                    .split(";")[i];
                                skuMap.add(goodsSku);
                              }
                              addToCar(json.encode(skuMap).toString());
                            },
                          ),
                        ),
                      ),
                      // new Container(
                      //   height: ScreenUtil.getInstance().setHeight(50),
                      //   decoration: new BoxDecoration(
                      //       color: Colors.orangeAccent,
                      //       borderRadius: BorderRadius.circular(
                      //           ScreenUtil.getInstance().setHeight(50))),
                      //   child: new FlatButton(
                      //     child: new Padding(
                      //       padding: EdgeInsets.all(0.0),
                      //       child: new Text(
                      //         "加入购物车",
                      //         style: new TextStyle(
                      //             color: Colors.white, fontSize: 14),
                      //       ),
                      //     ),
                      //     onPressed: () {
                      //       if (APPLIC.userInfoMap == null) {
                      //         Navigator.of(context).push(
                      //             new MaterialPageRoute(builder: (context) {
                      //           return new LoginPage();
                      //         }));
                      //         return;
                      //       }
                      //       if (APPLIC.userInfoMap['user_msg']['group_id'] ==
                      //           "1") {
                      //         Fluttertoast.showToast(msg: "请先升级成店主");
                      //         return;
                      //       }
                      //       if (provider.selectedSku.contains("请选择") &&
                      //           hasSku == false) {
                      //         showGoodsAttributes(context);
                      //       } else {
                      //         if (hasSku) {
                      //           addToCar("");
                      //           return;
                      //         }
                      //         List<Map> skuMap = List();
                      //         List<Goods> list = goodsDetailsModel.goods;
                      //         for (int i = 0; i < list.length; i++) {
                      //           Map goodsSku = new Map();
                      //           goodsSku['attribute_id'] = list[i].attribute_id;
                      //           goodsSku['value'] =
                      //               provider.selectedSkuModel.sku.split(";")[i];
                      //           skuMap.add(goodsSku);
                      //         }
                      //         addToCar(json.encode(skuMap).toString());

                      //         // // print(json.encode(skuMap).toString());
                      //         // Navigator.of(context)
                      //         //     .push(MaterialPageRoute(builder: (context) {
                      //         //   return BuyGoodsPage(
                      //         //     goods_id: widget.goods_id,
                      //         //     goods_name: goods_name,
                      //         //     goods_img: provider.selectedSkuModel.img,
                      //         //     price: provider.selectedSkuModel.price
                      //         //         .toString(),
                      //         //     skuJson: json.encode(skuMap).toString(),
                      //         //     skuInfo: provider.selectedSkuModel.sku,
                      //         //   );
                      //         // }));
                      //       }
                      //     },
                      //   ),
                      // ),
                      Container(
                        width: 10,
                      ),
                      new Container(
                        height: ScreenUtil.getInstance().setHeight(50),
                        decoration: new BoxDecoration(
                          color: mainColor,
                            borderRadius: BorderRadius.circular(
                                ScreenUtil.getInstance().setHeight(8))),
                        child: new FlatButton(
                          child: new Padding(
                            padding: EdgeInsets.all(0.0),
                            child: new Text(
                              "立即下单",
                              style: new TextStyle(
                                  color: Colors.white,
                                  fontSize: ScreenUtil.getInstance()
                                      .setSp(SMALL_FONT_SIZE)),
                            ),
                          ),
                          onPressed: () {
                            // if (APPLIC.userInfoMap == null) {
                            //    Navigator.of(context).pushNamed('login');
                            //   return;
                            // }
                            // if (APPLIC.userInfoMap['user_msg']['group_id']
                            //         .toString() ==
                            //     "1") {
                            //   Navigator.of(context).push(
                            //       new MaterialPageRoute(builder: (context) {
                            //     return new FillAuthCodePage();
                            //   }));
                            //   return;
                            // }
                            if (provider.selectedSku.contains("请选择") &&
                                hasSku == false) {
                              showGoodsAttributes(context);
                            } else {
                              if (hasSku) {
                                Navigator.of(context)
                                    .push(MaterialPageRoute(builder: (context) {
                                  return BuyGoodsPage(
                                    old: '',
                                    postage:freight.toString(),
                                    goods_num: goods_num.toString(),
                                    goods_id: widget.goods_id,
                                    goods_name: goods_name,
                                    goods_img: gallery[0].img,
                                    type: widget.type.toString(),
                                    id: widget.id,
                                    price: price.toString(),
                                    skuJson: "",
                                    skuInfo: "无配置",
                                  );
                                }));
                                return;
                              }
                              List<Map> skuMap = List();
                              List<Goods> list = goodsDetailsModel.goods;
                              for (int i = 0; i < list.length; i++) {
                                Map goodsSku = new Map();
                                goodsSku['attribute_id'] = list[i].attribute_id;
                                goodsSku['value'] =
                                    provider.selectedSkuModel.sku.split(";")[i];
                                skuMap.add(goodsSku);
                              }
                              // print(json.encode(skuMap).toString());
                              Navigator.of(context)
                                  .push(MaterialPageRoute(builder: (context) {
                                return BuyGoodsPage(
                                  old: '',
                                  postage:freight.toString(),
                                  goods_num: goods_num.toString(),
                                  type: widget.type.toString(),
                                  goods_id: widget.goods_id,
                                  goods_name: goods_name,
                                  id: widget.id,
                                  goods_img: provider.selectedSkuModel.img,
                                  price: provider.selectedSkuModel.price
                                      .toString(),
                                  skuJson: json.encode(skuMap).toString(),
                                  skuInfo: provider.selectedSkuModel.sku,
                                );
                              }));
                            }
                          },
                        ),
                      )
                    ],
                  ),
                )
              ],
            );
          })),
    );
  }

  showGoodsAttributes(BuildContext context) {
    showModalBottomSheet(
        context: context,
        backgroundColor: Colors.transparent,
        builder: (BuildContext context) {
          return StatefulBuilder(
            builder: (BuildContext context, setState) {
              return new Container(
                height: MediaQuery.of(context).size.height * 2 / 3,
                alignment: AlignmentDirectional.center,
                padding: EdgeInsets.fromLTRB(20, 0, 10, 10),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(15),
                      topRight: Radius.circular(15),
                    ),
                    color: white),
                child: new Column(
                  children: <Widget>[
                    new Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
                        GestureDetector(
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                          child: Container(
                            width: 50,
                            color: white,
                            height: 50,
                            child: Image.asset('images/icon_close.png'),
                          ),
                        )
                      ],
                    ),
                    Expanded(
                      flex: 1,
                      child: SingleChildScrollView(
                        child: ChangeNotifierProvider.value(
                            value: skuProvider,
                            child: SkuDialog(
                              goods_name: goods_name,
                              initPrice: price.toString(),
                              img: gallery[0].img,
                            )),
                      ),
                    ),
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: Text('数量',
                              maxLines: 1,
                              style: TextStyle(
                                  fontSize: ScreenUtil.getInstance()
                                      .setSp(SMALL_FONT_SIZE),
                                  color: black)),
                        ),
                        GestureDetector(
                          child: Icon(
                            Icons.remove,
                            color: black,
                            size: ScreenUtil.getInstance().setHeight(DP_30),
                          ),
                          onTap: () {
                            if (goods_num == 1) {
                              return;
                            }
                            goods_num--;
                            setState(() {});
                          },
                        ),
                        Container(
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(2),
                              color: f0Color),
                          padding: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_20),
                              2,
                              ScreenUtil.getInstance().setHeight(DP_20),
                              2),
                          margin: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_10),
                              0,
                              ScreenUtil.getInstance().setHeight(DP_10),
                              0),
                          child: Text(
                            goods_num.toString(),
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(SMALL_FONT_SIZE),
                                color: Colors.black87),
                          ),
                        ),
                        GestureDetector(
                          child: Icon(
                            Icons.add,
                            size: ScreenUtil.getInstance().setHeight(DP_30),
                            color: black,
                          ),
                          onTap: () {
                            goods_num++;
                            setState(() {});
                          },
                        )
                      ],
                    ),
                    Container(
                      height: 10,
                    ),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Offstage(
                          offstage: widget.type != null,
                          child: new Container(
                            margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                            height: ScreenUtil.getInstance().setHeight(50),
                            decoration: new BoxDecoration(
                                color: Colors.orangeAccent,
                                borderRadius: BorderRadius.circular(
                                    ScreenUtil.getInstance().setHeight(5))),
                            child: new FlatButton(
                              child: new Padding(
                                padding: EdgeInsets.all(0.0),
                                child: new Text(
                                  "加入购物车",
                                  style: new TextStyle(
                                      color: Colors.white,
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(SMALL_FONT_SIZE)),
                                ),
                              ),
                              onPressed: () {
                                // Navigator.of(context).pop();
                                if (hasSku) {
                                  addToCar("");
                                  return;
                                }
                                List<Map> skuMap = List();
                                List<Goods> list = goodsDetailsModel.goods;
                                for (int i = 0; i < list.length; i++) {
                                  Map goodsSku = new Map();
                                  goodsSku['attribute_id'] =
                                      list[i].attribute_id;
                                  goodsSku['value'] = skuProvider
                                      .selectedSkuModel.sku
                                      .split(";")[i];
                                  skuMap.add(goodsSku);
                                }
                                addToCar(json.encode(skuMap).toString());
                              },
                            ),
                          ),
                        ),
                        new Container(
                          height: ScreenUtil.getInstance().setHeight(50),
                          margin: EdgeInsets.fromLTRB(20, 10, 0, 10),
                          decoration: new BoxDecoration(
                             color: mainColor,
                              borderRadius: BorderRadius.circular(
                                  ScreenUtil.getInstance().setHeight(5))),
                          child: new FlatButton(
                            child: new Padding(
                              padding: EdgeInsets.all(0.0),
                              child: new Text(
                                "立即下单",
                                style: new TextStyle(
                                    color: Colors.white,
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(SMALL_FONT_SIZE)),
                              ),
                            ),
                            onPressed: () {
                              if (APPLIC.userInfoMap == null) {
                                Navigator.of(context).pushNamed('login');
                                return;
                              }
                              if (APPLIC.userInfoMap['user_msg']['group_id']
                                      .toString() ==
                                  "1") {
                                Navigator.of(context).push(
                                    new MaterialPageRoute(builder: (context) {
                                  return new FillAuthCodePage();
                                }));
                                return;
                              }
                              if (hasSku) {
                                Navigator.of(context).push<String>(
                                    MaterialPageRoute(builder: (context) {
                                  return BuyGoodsPage(
                                    old: old_price,
                                    goods_id: widget.goods_id,
                                    type: widget.type.toString(),
                                    goods_name: goods_name,
                                    goods_num: goods_num.toString(),
                                    goods_img: gallery[0].img,
                                    price: price.toString(),
                                    skuJson: "",
                                    id: widget.id,
                                    skuInfo: "无配置",
                                  );
                                })).then((value) {
                                  if (value != null) {
                                    Navigator.of(context).push(
                                        new MaterialPageRoute(
                                            builder: (context) {
                                      return new OrderPage();
                                    }));
                                  }
                                });
                                return;
                              }
                              List<Map> skuMap = List();
                              List<Goods> list = goodsDetailsModel.goods;
                              for (int i = 0; i < list.length; i++) {
                                Map goodsSku = new Map();
                                goodsSku['attribute_id'] = list[i].attribute_id;
                                goodsSku['value'] = skuProvider
                                    .selectedSkuModel.sku
                                    .split(";")[i];
                                skuMap.add(goodsSku);
                              }
                              // print(json.encode(skuMap).toString());
                              Navigator.of(context).push<String>(
                                  MaterialPageRoute(builder: (context) {
                                return BuyGoodsPage(
                                  old: old_price,
                                  goods_num: goods_num.toString(),
                                  goods_id: widget.goods_id,
                                  type: widget.type.toString(),
                                  goods_name: goods_name,
                                  goods_img: skuProvider.selectedSkuModel.img,
                                  price: skuProvider.selectedSkuModel.price
                                      .toString(),
                                  id: widget.id,
                                  skuJson: json.encode(skuMap).toString(),
                                  skuInfo: skuProvider.selectedSkuModel.sku,
                                );
                              })).then((value) {
                                if (value != null) {
                                  Navigator.of(context).push(
                                      new MaterialPageRoute(builder: (context) {
                                    return new OrderPage();
                                  }));
                                }
                              });
                              // Navigator.of(context).pop();
                            },
                          ),
                        )
                      ],
                    ),
                  ],
                ),
              );
            },
          );
        });
  }

  addToCar(String attr) async {
    // Map map = new Map();
    // map['token'] = token;
    // map['goods_id'] = widget.goods_id;
    // map['sign_type'] = SignType.ADD_SHOP_CAR;
    // map['goods_num'] = goods_num;
    // if (attr != "") map['goods_sku'] = attr;
    // Response<String> response =
    //     await HttpUtil().post(Api.ADD_SHOP_CAR, data: getSign(map));
    // Map userMap = json.decode(response.data);
    // int code = userMap['code'];
    // Fluttertoast.showToast(msg: userMap['msg']);
    // if (code == 1 && mounted) {}
  }
}
